package com.ukao.steward.ui.menuLeft.myEarnings;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.ukao.steward.R;
import com.ukao.steward.adapter.ToDayListAdapter;
import com.ukao.steward.base.MvpFragment;
import com.ukao.steward.bean.TodayEarningBean;
import com.ukao.steward.dialog.PickerViewDialog;
import com.ukao.steward.listener.OnItemClickListener;
import com.ukao.steward.pesenter.valetRunners.TodayEarningsPesenter;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.DensityUtils;
import com.ukao.steward.util.MyDateUtils;
import com.ukao.steward.view.valetRunners.TodayEarningsView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 日收成明细
 */
public class TodayEarningsFragment extends MvpFragment<TodayEarningsPesenter> implements TodayEarningsView {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.tab_layout)
    LinearLayout tabLayout;
    @BindView(R.id.tab_layout1)
    LinearLayout tabLayout1;
    @BindView(R.id.tab_layout2)
    LinearLayout tabLayout2;

    Unbinder unbinder;
    @BindView(R.id.tab_count)
    TextView tabCount;
    @BindView(R.id.tab_msg)
    TextView tabMsg;
    @BindView(R.id.tab_count1)
    TextView tabCount1;
    @BindView(R.id.tab_msg1)
    TextView tabMsg1;
    @BindView(R.id.tab_count2)
    TextView tabCount2;
    @BindView(R.id.tab_msg2)
    TextView tabMsg2;
    @BindView(R.id.before_txt)
    TextView beforeTxt;
    @BindView(R.id.date_txt)
    TextView dateTxt;
    @BindView(R.id.later_txt)
    TextView laterTxt;
    @BindView(R.id.earnings_msg)
    TextView earningsMsg;
    @BindView(R.id.amount)
    TextView amount;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;


    private String dateType;

    private TextView[] mTextView;
    private TextView[] mTextView1;
    private TextView[] mTextView2;
    private LinearLayout[] mLinearLayout;
    private String date;
    private Calendar now;
    private String TimeType;
    private int amountParam;
    private int state = 0;
    private TodayEarningBean mTodayEarningBean;
    private ToDayListAdapter mToDayListAdapter;
    private List<TodayEarningBean.IncomeListBean>   mIncomeListBean=new ArrayList<>();
    boolean  isloadFinish=false;
    public static TodayEarningsFragment newInstance(String param1, int amount) {
        TodayEarningsFragment fragment = new TodayEarningsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putInt(ARG_PARAM2, amount);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            dateType = getArguments().getString(ARG_PARAM1);
            amountParam = getArguments().getInt(ARG_PARAM2, 0);
        }
    }

    @Override
    protected TodayEarningsPesenter createPresenter() {
        return new TodayEarningsPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_today_earnings, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }

    @Override
    protected void initView() {

        now = Calendar.getInstance();
        switch (dateType) {
            case "1":
                title.setText("日收成明细");
                break;
            case "2":
                earningsMsg.setText("月总收成 (元)");
                title.setText("月收成明细");
                beforeTxt.setText("上个月");
                laterTxt.setText("下个月");

                break;
        }

        titleLayout.setBackgroundColor(getColors(R.color.colorPrimary));
        mTextView = new TextView[]{tabCount, tabMsg};
        mTextView1 = new TextView[]{tabCount1, tabMsg1};
        mTextView2 = new TextView[]{tabCount2, tabMsg2};
        mLinearLayout = new LinearLayout[]{tabLayout,tabLayout1, tabLayout2};
        for (int i = 0; i < mLinearLayout.length; i++) {

            ViewGroup.LayoutParams mParam = mLinearLayout[i].getLayoutParams();
            mParam.width = DensityUtils.getScreenW() / mLinearLayout.length;
            mLinearLayout[i].setLayoutParams(mParam);

        }
        initLinearRecyclerView(recyclerView);
         mToDayListAdapter= new ToDayListAdapter(_mActivity,mIncomeListBean );
        mToDayListAdapter.setOnItemClickListener(mOnItemClickListener);
        recyclerView.setAdapter(mToDayListAdapter);
    }


    @Override
    protected void initListener() {
        super.initListener();

    }

    private   OnItemClickListener  mOnItemClickListener=new OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            TodayEarningBean.IncomeListBean mitem = mToDayListAdapter.getItem(position);
           String  msg="";
            if (state==0){
                msg="提成";
            }else{
                msg="明细";
            }

       String     titleMsg=getText(dateTxt)+mitem.getName()+msg;
            start(BalanceListFragment.newInstance(mitem.getType(),dateType,date,titleMsg,state));
        }
    };

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (isloadFinish)return;
        formatDateCustom();
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.back_btn, R.id.before_txt, R.id.date_txt, R.id.later_txt, R.id.tab_layout, R.id.tab_layout1, R.id.tab_layout2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.before_txt:

                switch (dateType) {
                    case "1":/*年月日*/


                        now = MyDateUtils.getBeforeDay(now);
                        formatDateCustom();
                        break;
                    case "2":
                        now = MyDateUtils.getBeforeMouth(now);
                        formatDateCustom();

                        break;
                }

                break;
            case R.id.date_txt:
                String birthday = getText(dateTxt);
                switch (dateType) {
                    case "1":/*月日*/


                        PickerViewDialog.getInstance().showAnalyzeDatePicker(birthday, TimeType, true, _mActivity, mOnTimeSelectListener);
                        break;
                    case "2":

                        PickerViewDialog.getInstance().showAnalyzeDatePicker(birthday, TimeType, false, _mActivity, mOnTimeSelectListener);
                        break;
                }
                break;
            case R.id.later_txt:
                switch (dateType) {
                    case "1":/*年月日*/
                        String currenData = MyDateUtils.getDateTime(MyDateUtils.YEAR_MOUTH_DAY1);
                        if (currenData.compareTo(date) > 0) {
                            now = MyDateUtils.getAfterDay(now);
                            formatDateCustom();
                        } else {

                        }


                        break;
                    case "2":

                        String currenData1 = MyDateUtils.getDateTime("yyyy-MM");
                        if (currenData1.compareTo(date) > 0) {
                            now = MyDateUtils.getAfterMonth(now);
                            formatDateCustom();
                        }

                        break;
                }
                break;
            case R.id.tab_layout:
                switchState(state = 0);
                break;
            case R.id.tab_layout1:
                switchState(state = 1);
                break;
            case R.id.tab_layout2:
                switchState(state = 2);
                break;
        }
    }


    private void formatDateCustom() {

        String currenData = "";
        switch (dateType) {
            case "1":/*年月日*/
                TimeType = MyDateUtils.YEAR_MOUTH_DAY;

                break;
            case "2":
                TimeType = MyDateUtils.YEAR_MOUTH;

                break;
        }
        currenData = MyDateUtils.getDateTime(MyDateUtils.YEAR_MOUTH_DAY1);
        date = MyDateUtils.formatDateCustom(now.getTime(), MyDateUtils.YEAR_MOUTH_DAY1);
        laterTxt.setVisibility(currenData.compareTo(date) == 0 ? View.GONE : View.VISIBLE);
        dateTxt.setText(MyDateUtils.formatDateCustom(now.getTime(), TimeType));
        mvpPresenter.myIncome(date,dateType);

    }


    private void switchState(int type) {
        for (int i = 0; i < mLinearLayout.length; i++) {
            if (type == i) {
                mLinearLayout[i].setBackgroundColor(getColors(R.color.earnings_color));
            } else {
                mLinearLayout[i].setBackgroundColor(getColors(R.color.white));
            }


        }

        switch (type) {
            case 0:
                for (TextView textView : mTextView) {
                    textView.setTextColor(getColors(R.color.normal_blue));
                }
                for (TextView textView : mTextView1) {
                    textView.setTextColor(getColors(R.color.black));
                }

                for (TextView textView : mTextView2) {
                    textView.setTextColor(getColors(R.color.black));
                }

                break;
            case 1:
                for (TextView textView : mTextView) {
                    textView.setTextColor(getColors(R.color.black));
                }
                for (TextView textView : mTextView1) {
                    textView.setTextColor(getColors(R.color.normal_blue));
                }

                for (TextView textView : mTextView2) {
                    textView.setTextColor(getColors(R.color.black));
                }

                break;

            case 2:
                for (TextView textView : mTextView) {
                    textView.setTextColor(getColors(R.color.black));
                }
                for (TextView textView : mTextView1) {
                    textView.setTextColor(getColors(R.color.black));
                }

                for (TextView textView : mTextView2) {
                    textView.setTextColor(getColors(R.color.normal_blue));
                }
                break;

        }

        setSateData();
    }

    private TimePickerView.OnTimeSelectListener mOnTimeSelectListener = new TimePickerView.OnTimeSelectListener() {
        @Override
        public void onTimeSelect(Date date, View v) {

            now.setTime(date);
            formatDateCustom();
        }
    };


    /**
     * 收入信息
     *
     * @param mTodayEarningBean
     */
    @Override
    public void todayEarningSucceed(TodayEarningBean mTodayEarningBean) {
        isloadFinish=true;
        this.mTodayEarningBean = mTodayEarningBean;
        tabCount.setText(CheckUtils.isEmptyNumber(mTodayEarningBean.getIncome()));
        amount.setText(CheckUtils.isEmptyNumber(mTodayEarningBean.getTotal()));
        tabCount2.setText(CheckUtils.isEmptyNumber(mTodayEarningBean.getOther()));
        setSateData();
    }


    @Override
    public void showError(String error) {
        super.showError(error);
        isloadFinish=false;
    }

    private void setSateData() {
        if (mTodayEarningBean==null)return;
        switch (state) {
            case 0:

                mToDayListAdapter.setDataList(mTodayEarningBean.getIncomeList());
                break;
            case 2:
                mToDayListAdapter.setDataList(mTodayEarningBean.getOtherList());
                break;
        }
    }
}
